//
//  ParentViewModelVC.m
//  YZZX
//
//  Created by Easaa on 15/4/20.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ParentViewModelVC.h"
#import "CenterOverviewMassge.h"

@interface ParentViewModelVC ()<UIScrollViewDelegate>
{
    NSArray         *arryViewData;    //view 的mdoel
    
    UIView          * LineView;         //but下得下划线
    
    UIView          *NavView;            //but 容器view
    UIButton        *ButTagColor;         //记录上一次点击按钮的指针
}

@end

@implementation ParentViewModelVC

-(id)initWithParentCateGoryData:(CategoryDataModel *)categoryModel;
{
    self= [super init];
    if(self)
    {
        self.ModeData = categoryModel;
        
        
    }
    
    return self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   // self.view.backgroundColor = [UIColor redColor];
    
    
    arryViewData = self.ModeData.children;
    
    
    self.Bgscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64+40, Dev_ScreenWidth, Dev_ScreenHeight-64-40-49)];
    self.Bgscrollview.backgroundColor =[UIColor whiteColor];
    self.Bgscrollview.delegate = self;
    self.Bgscrollview.contentSize = CGSizeMake(Dev_ScreenWidth*arryViewData.count, Dev_ScreenHeight-64-40-49);
    self.Bgscrollview.bounces = NO;
    [self.Bgscrollview setPagingEnabled:YES];
    [self.Bgscrollview setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:self.Bgscrollview];

  
    
     [self TableviewNewView:arryViewData];
     [self NavViewButView];
    
    
    
}
#pragma mark 实例化多个TableView
-(void)TableviewNewView:(NSArray *)dataArry
{
//    if(dataArry.count==1)
//    {
//         CategoryDataModel *tableviewmodel =dataArry[0];
//        
//        MassgeViewDanGe *tablview = [[MassgeViewDanGe alloc]initW
//        tablview.tag = i;
//        
//        tablview.ListTableviewdelegate =self.rootviews;
//        
//        [self.Bgscrollview addSubview:tablview];
//
//        
//        
//     
//        return;
//    }
    

    for (int i =0 ;i<dataArry.count ;i++)
    {
        CategoryDataModel *tableviewmodel =dataArry[i];
        
        if(![tableviewmodel.cate_id isEqualToString:@"6"])
        {
            ListTableviewModel *tablview = [[ListTableviewModel alloc]initWithFram:CGRectMake(Dev_ScreenWidth*i, 0,Dev_ScreenWidth , Dev_ScreenHeight-64-48-40) CategoryModel:tableviewmodel];
            tablview.tag = i;
             tablview.ListTableviewdelegate =self.rootviews;
            
            [self.Bgscrollview addSubview:tablview];
            
        }else if ([tableviewmodel.cate_id isEqualToString:@"6"])
        {
            CenterOverviewMassge  *yueziOverview =[[CenterOverviewMassge alloc]initWithFram:CGRectMake(Dev_ScreenWidth*i, 0,Dev_ScreenWidth , Dev_ScreenHeight-64-48-40) CategoryModel:tableviewmodel];
        
             [self.Bgscrollview addSubview:yueziOverview];
        }
        
        
    }
    
    

}


#pragma mark 创建but导航二级栏目
-(void)NavViewButView
{

    NavView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, Dev_ScreenWidth, 40)];
    NavView.backgroundColor = [UIColor whiteColor];
    for (int i =0; i<arryViewData.count; i++)
    {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(Dev_ScreenWidth/arryViewData.count*i, 0, Dev_ScreenWidth/arryViewData.count, 40);
        [but addTarget:self action:@selector(Butfantion:) forControlEvents:UIControlEventTouchUpInside];
        but.titleLabel.font = [UIFont systemFontOfSize:11];
        but.tag = i;
        CategoryDataModel * ModelView=arryViewData[i];
        [but setTitle:ModelView.cate_name forState:UIControlStateNormal];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [NavView addSubview:but];
        if(i == 0)
        {
            [but setTitleColor:RGBCOLOR(41, 112, 163) forState:UIControlStateNormal];
            ButTagColor = but;
        }
        
    }
    
    LineView = [[UIView alloc]initWithFrame:CGRectMake(0, 39,Dev_ScreenWidth/arryViewData.count, 1)];
    LineView.backgroundColor = RGBCOLOR(252, 115, 148);
    LineView.hidden = NO;
    LineView.tag = 102411;
    [NavView addSubview:LineView];
    [self.view addSubview:NavView];
    
    
}

#pragma mark  but导航二级栏目方法
-(void)Butfantion:(UIButton *)but
{
    LineView.frame = CGRectMake(Dev_ScreenWidth/arryViewData.count*but.tag, 39, Dev_ScreenWidth/arryViewData.count, 1);
    
    self.Bgscrollview.contentOffset = CGPointMake(Dev_ScreenWidth*but.tag, 0);
    
    [self ButtonChangTitleColor:but];
}
#pragma mark  but导航二级栏目方法 ---外部跳转方法
-(void)ButfantionNavTiaozhuan:(NSInteger )Tag
{
    LineView.frame = CGRectMake(Dev_ScreenWidth/arryViewData.count*Tag, 39, Dev_ScreenWidth/arryViewData.count, 1);
    
    self.Bgscrollview.contentOffset = CGPointMake(Dev_ScreenWidth*Tag, 0);
    
    for (UIButton *butindex in [NavView subviews])
    {
        if(butindex.tag == Tag)
        {
            [self ButtonChangTitleColor:butindex];
            
        }
    }
}




-(void)ButtonChangTitleColor:(UIButton *)but
{
    [ButTagColor setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [but setTitleColor:RGBCOLOR(41, 112, 163)  forState:UIControlStateNormal];
    ButTagColor = but;
}


#pragma mark  UIScrollView代理
-(void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    if(scrollView == self.Bgscrollview)
    {
        
        
        
    }
    
    
}


//当上面提到的减速完毕、滚动视图停止时得到通知。收到这个通知的时刻，滚动视图的contentOffset属性会反映出滚动条最终停止的位置。
-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
    
    if(scrollView == self.Bgscrollview)
    {
        
        int nuber =scrollView.contentOffset.x/Dev_ScreenWidth;
        
        LineView.frame = CGRectMake(Dev_ScreenWidth/arryViewData.count*nuber, 39, Dev_ScreenWidth/arryViewData.count, 1);
        
        
        for (UIButton *butNew in NavView.subviews)
        {
            if (butNew.tag ==nuber )
            {
            [self ButtonChangTitleColor:butNew];
            }
        }
    }
    
}







@end
